package com.lombardisoftware.portal.action;

import com.lombardisoftware.logger.WLELoggerConstants;
import com.lombardisoftware.portal.helpers.ProcessAPIHelper;
import com.lombardisoftware.portal.struts.ActionWithFailure;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;

public class AbortInstance extends ActionWithFailure
{
  private static Logger logger = Logger.getLogger(WLELoggerConstants.PORTAL_LOGGER, "WLEPIIMessages");
  private static final String CLASS_NAME = AbortInstance.class.getName();

  public ActionForward execute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest req, HttpServletResponse resp)
    throws Exception
  {
    DynaActionForm form = (DynaActionForm)actionForm;

    String[] idStrings = form.getStrings("id");

    List instanceIds = new ArrayList(idStrings.length);
    for (int i = 0; i < idStrings.length; ++i) {
      instanceIds.add(new BigDecimal(idStrings[i]));
    }

    int modified = ProcessAPIHelper.getInstance().abortInstances(instanceIds);
    if (modified != instanceIds.size()) {
      recordFailure(req, "abort.instance.failure", instanceIds.size(), modified);
      return mapping.findForward("FAILURE");
    }

    return mapping.findForward("SUCCESS");
  }
}